﻿using D3StatsClient.Model;
using D3StatsClient.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using System.Xml.Linq;

namespace D3StatsClient.Commands
{
    public class UploadSession : CommandBase
    {
        public override bool CanExecute(object parameter)
        {
            //Change this here to return true only if the session has not already been uploaded.
            SessionPickerEntry spe = parameter as SessionPickerEntry;

            if (spe == null)
                return false;

            if (spe.IsUploaded || spe.IsUploading)
            {
                return false;
            }

            return true;
        }

        public override void Execute(object parameter)
        {
            SessionPickerEntry spe = parameter as SessionPickerEntry;
            
            Task.Factory.StartNew(() =>
            {
                try
                {
                    spe.IsUploading = true;
                    this.ForceInvalidateRequerySuggested();

                    Thread.Sleep(4000);

                    //Upload session to web api.
                    using (var client = new HttpClient())
                    {
                        using (var content = new MultipartFormDataContent())
                        {
                            var fileContent = new StreamContent(File.OpenRead(spe.XmlDataFile));

                            content.Add(fileContent);

                            var requestUri = string.Format(Properties.Settings.Default.UploadServiceUrl, Properties.Settings.Default.UserSecurityToken);

                            var task = client.PostAsync(requestUri, content);
                            task.Wait();

                            var result = task.Result;

                            var spvm = ViewModelManager<SessionPickerWindowViewModel>.ViewModel;

                            if (result.StatusCode == System.Net.HttpStatusCode.Created)
                            {
                                //success.
                                XDocument doc = XDocument.Load(spe.XmlDataFile);

                                doc.Element("items").Add(new XAttribute("IsUploaded", true));
                                doc.Save(spe.XmlDataFile);

                                spe.IsUploaded = true;

                                //spvm.Refresh();
                            }
                            else if (result.StatusCode == System.Net.HttpStatusCode.Conflict)
                            {
                                //if we get this status, it means that this session has already
                                //been uploaded, or at least exists in the database.
                                XDocument doc = XDocument.Load(spe.XmlDataFile);

                                doc.Element("items").Add(new XAttribute("IsUploaded", true));
                                doc.Save(spe.XmlDataFile);

                                spe.IsUploaded = true;

                                //spvm.Refresh();
                            }
                        }
                    }
                }
                finally
                {
                    spe.IsUploading = false;
                    this.ForceInvalidateRequerySuggested();
                }
            });
        }
    }
}
